package com.yunhe.fineart.view;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.RelativeLayout;

import com.yunhe.fineart.R;


public class GridRelativeLayout extends RelativeLayout {
    private Canvas myCanvas;
    private int horGrid = 5, verGrid = 5;//水平网格和竖直网格
    private int screenW, screenH;//屏幕宽和高
    private boolean initOver = false;//初始化标签

    public GridRelativeLayout(Context context) {
        super(context);
        setWillNotDraw(false);
    }

    public GridRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
    }

    public GridRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setWillNotDraw(false);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        this.myCanvas = canvas;
        Log.i("gd","draw>>>>>>");
        Paint paint = new Paint();
        paint.setColor(Color.BLUE);//颜色
        paint.setStrokeWidth(3);//线宽
//        int verNum = (int)(screenH / verGrid) + 1;
//        int horNum = (int)(screenW / horGrid) + 1;
//        if (initOver) {
//            for (int i = 0; i < verNum; i++) {
//                canvas.drawLine(0, i * verGrid - 1, screenW, i * verGrid - 1,
//                        paint);
//            }
//            for (int i = 0; i < horNum; i++) {
//                canvas.drawLine(i * horGrid - 1, 0, i * horGrid - 1, screenH,
//                        paint);
//            }
//        }
        if(initOver){
            DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
            int screenWidth = metrics.widthPixels;
            int screenHeight = (int) (metrics.heightPixels);

            // Set paint options
            paint.setAntiAlias(true);
            paint.setStrokeWidth(3);
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.BLUE);

            int top = getResources().getDimensionPixelSize(R.dimen.height_50);
            //竖屏
            if(screenHeight>screenWidth){
                //画竖线
                for(int i = 0; i < verGrid-1; i++){
                    int x0 = (screenWidth/verGrid)*(i+1);
                    canvas.drawLine(x0,top,x0,screenHeight,paint);
                }
                int itemWidth=screenWidth/verGrid;
                int horCount = screenHeight/itemWidth;
                //画横线
                for(int i = 0; i < horCount-1; i++){
                    int y0 = (screenHeight/horCount)*(i+1);
                    canvas.drawLine(0,y0+top,screenWidth,y0+top,paint);
                }
            }
            else {//横屏
                //画横线
                for(int i = 0; i < horGrid-1; i++){
                    int y0 = (screenHeight/horGrid)*(i+1);
                    canvas.drawLine(0,y0+top,screenWidth,y0+top,paint);
                }
                int itemHeight=screenHeight/horGrid;
                int verCount = screenWidth/itemHeight;

                //画竖线
                for(int i = 0; i < verCount-1; i++){
                    int x0 = (screenWidth/verCount)*(i+1);
                    canvas.drawLine(x0,top,x0,screenHeight,paint);
                }
            }
            //画横线
//            for(int i = 0; i < horGrid-1; i++){
//                int y0 = (screenHeight/horGrid)*(i+1);
//                canvas.drawLine(0,y0+top,screenWidth,y0+top,paint);
//            }
//            canvas.drawLine(0,(screenHeight/5)*1+top,screenWidth,(screenHeight/5)*1+top,paint);
//            canvas.drawLine(0,(screenHeight/5)*2+top,screenWidth,(screenHeight/5)*2+top,paint);
//            canvas.drawLine(0,(screenHeight/5)*3+top,screenWidth,(screenHeight/5)*3+top,paint);
//            canvas.drawLine(0,(screenHeight/5)*4+top,screenWidth,(screenHeight/5)*4+top,paint);
            //画竖线
//            for(int i = 0; i < verGrid-1; i++){
//                int x0 = (screenWidth/verGrid)*(i+1);
//                canvas.drawLine(x0,top,x0,screenHeight,paint);
//            }
//            canvas.drawLine((screenWidth/5)*1,top,(screenWidth/5)*1,screenHeight,paint);
//            canvas.drawLine((screenWidth/5)*2,top,(screenWidth/5)*2,screenHeight,paint);
//            canvas.drawLine((screenWidth/5)*3,top,(screenWidth/5)*3,screenHeight,paint);
//            canvas.drawLine((screenWidth/5)*4,top,(screenWidth/5)*4,screenHeight,paint);


        }
    }

    /**设置网格线參数**/
    public void setInf(int vergrid, int horgrid, int screenW, int screenH) {
        this.verGrid = vergrid;
        this.horGrid = horgrid;
        this.screenW = screenW;
        this.screenH = screenH;
        initOver = true;
        postInvalidate();
    }

    /**擦除网格线**/
    public void clearLine()
    {
        initOver = false;
        postInvalidate();
    }
}



